package com.ruoyi.monthlyInventory.controller;


import com.ruoyi.audit.service.AuditService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.monthlyInventory.domain.MonthlyInventory;
import com.ruoyi.monthlyInventory.domain.MonthlyInventoryLog;
import com.ruoyi.monthlyInventory.service.MonthlyInventoryService;
import com.ruoyi.warehousing.domain.Warehousing;
import com.ruoyi.warehousing.domain.WarehousingRecords;
import com.ruoyi.warehousing.service.WarehousingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 月度管理Controller
 *
 * @author zjh
 * @date 2022-06-06
 */
@Controller
@RequestMapping("/monthlyInventory")
public class MonthlyInventoryController extends BaseController {
    private String prefix = "monthlyInventory";

    @Autowired
    private MonthlyInventoryService monthlyInventoryService;

    @Autowired
    private AuditService auditService;

    @Autowired
    private WarehousingService warehousingService;

    @RequiresPermissions("business:monthlyInventory:view")
    @GetMapping()
    public String manufacturer(ModelMap mmap)
    {
        List<MonthlyInventory> list = monthlyInventoryService.selectMonthlyInventoryList(new MonthlyInventory());
        if(list.size() != 0){
            mmap.put("code",0);
        }
        return prefix + "/monthlyInventory";
    }

    /**
     * 查询月度盘点列表
     */
    @RequiresPermissions("business:monthlyInventory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MonthlyInventory monthlyInventory)
    {
        startPage();
        List<MonthlyInventory> list = monthlyInventoryService.selectMonthlyInventoryList(monthlyInventory);
        return getDataTable(list);
    }

    /**
     * 新增月度盘点
     */
    @RequiresPermissions("business:monthlyInventory:add")
    @Log(title = "月度盘点", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MonthlyInventory monthlyInventory)
    {
        return toAjax(monthlyInventoryService.insertMonthlyInventorys(monthlyInventory));
    }

    /**
     * 修改保存月度盘点
     */
    @Log(title = "确认月度信息相符", businessType = BusinessType.UPDATE)
    @PostMapping("/equalsSure")
    @ResponseBody
    public AjaxResult equalsSure(Long monthlyInventoryId) {
        MonthlyInventory monthlyInventory=new MonthlyInventory();
        monthlyInventory.setMonthlyInventoryId(monthlyInventoryId);
        monthlyInventory.setMonthlyInventoryStatus(1);
        return toAjax( monthlyInventoryService.updateMonthlyInventory(monthlyInventory));
    }

    /**
     * 修改保存月度盘点
     */
    @Log(title = "确认月度信息不相符", businessType = BusinessType.UPDATE)
    @PostMapping("/notEqualsSure")
    @ResponseBody
    public AjaxResult notEqualsSure(Long monthlyInventoryId) {
        MonthlyInventory monthlyInventory=new MonthlyInventory();
        monthlyInventory.setMonthlyInventoryId(monthlyInventoryId);
        monthlyInventory.setMonthlyInventoryStatus(2);
        return toAjax( monthlyInventoryService.updateMonthlyInventory(monthlyInventory));
    }

    /**
     * 修改月度盘点
     */
    @GetMapping("/editIn")
    public String add(Long monthlyInventoryId,String modelNo, ModelMap mmap){
        mmap.put("monthlyInventoryId",monthlyInventoryId);
//        Warehousing warehousing= WarehousingService.sele
        MonthlyInventory monthlyInventory=monthlyInventoryService.selectMonthlyInventoryById(monthlyInventoryId);
        Warehousing warehousing = new Warehousing();
        WarehousingRecords warehousingRecords = new WarehousingRecords();
        warehousing.setBatch(monthlyInventory.getBatch());
        warehousing.setDeptId(monthlyInventory.getCompanyId());
        warehousing.setProductId(monthlyInventory.getProductId());
        warehousing.setProductType(modelNo);
        Warehousing warehousing1 = warehousingService.selectWarehousing(warehousing);
        mmap.put("num",warehousing1.getWarehousingNum());
        return prefix+"/applyInspect";
    }

    /**
     * 修改保存月度盘点
     */
    @Log(title = "申请修改月度库存信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(MonthlyInventoryLog monthlyInventoryLog) {
        return toAjax(monthlyInventoryService.insertMonthlyInventoryLog(monthlyInventoryLog));
    }

    /**
     * 修改公司培训记录
     */
    @GetMapping("/editLog")
    public String editLog(Long monthlyInventoryId,String yearMonthNyt, ModelMap mmap)
    {
        mmap.put("monthlyInventoryId",monthlyInventoryId);
        mmap.put("yearMonthNyt",yearMonthNyt);
        return prefix + "/logDel";
    }

    /**
     * 修改月度盘点
     */
    @PostMapping("/editLogDel")
    @ResponseBody
    public TableDataInfo editLogDel(String monthlyInventoryId,String yearMonthNyt ){
        MonthlyInventoryLog monthlyInventoryLog=new MonthlyInventoryLog();
        monthlyInventoryLog.setMonthlyInventoryId(Long.parseLong(monthlyInventoryId));
        String time=yearMonthNyt.replaceAll("-","_")+"_%";
        monthlyInventoryLog.setTheseTime(time);
        List<MonthlyInventoryLog> list=monthlyInventoryService.selectMonthlyInventoryLogList(monthlyInventoryLog);
        System.out.println(list);
        return getDataTable(list);
    }


}
